/**
 * Created by pactera on 16/12/28.
 */
module.exports = function ($rootScope,$scope,$stateParams,$state,$cookies,resetpasswordService){

    $scope.init = function () {

        $scope.password1 = "";
        $scope.password2 = "";
        var paras = getRequestParas();
        $scope.phoneNum = sessionStorage.getItem('phoneNum');
        $scope.emailNum = $stateParams.emailNum;

        $scope.token = paras.token;
        $scope.type = paras.type;
        // alert("phoneNum:" + $scope.phoneNum + ",emailNum:" + $scope.emailNum + ",token:" + $scope.token + ",type" + $scope.type);
        // $scope.emailNum = $stateParams.emailNum;
        // $scope.token = $stateParams.token;

    };

    $scope.init();
  /**
   * 获取Url中邮箱参数
   * @returns {*}
   * @constructor
   */
  function getRequestParas() {
    var res = new Object();
    var url = window.location.href; //获取url中"?"符后的字串
    if (url.indexOf("?") != -1) {
      url = url.substring(url.indexOf("?") + 1,url.length);
      var str = url.split('&');
      for(var index = 0; index < str.length + 1; index++) {
        var cc = str[index];
        if(index == 0) {

          res.token = cc.split('=')[1];
        }
        if(index == 1) {

          res.type = cc.split('=')[1];
        }
      }
    }
    return res;
  }
    $scope.reset = function(){
        $('.password1').removeClass('writeError');
        $('.password2').removeClass('writeError');
        $('.error_pwd').fadeOut();
        if ($scope.password1 == $scope.password2 && ($scope.password2!=''&&$scope.password1!='')){

            $rootScope.loadingShow = true;
            $rootScope.fadeShow = true;
            resetpasswordService.resetpassword($scope.password1, $scope.phoneNum, $scope.emailNum, $scope.token, $scope.type).then(function(data){

                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                if (data.code == 1){
                    $cookies.remove('PwdBenDi');
                    $state.go('loginHeader.login');
                }else {
                    $('.error_pwd').fadeIn();
                    $('.error_pwd').children('p').html(data.msg);
                }
            },function (response) {
                $rootScope.loadingShow = false;
                $rootScope.fadeShow = false;
                console.error('phoneVerify error', response.status, response.statusText);
            });
        }else {
            $('.password1').addClass('writeError');
            $('.password2').addClass('writeError');
            $('.error_pwd').fadeIn();
        }
    };
};
